/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package webtest;

import java.io.*;
import java.net.*;
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamPanel;
import com.github.sarxos.webcam.WebcamResolution;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

class UDPSender {

    static byte[] stream;
    static Webcam webcam = Webcam.getDefault();
    //klient 1

    public static void main(String args[]) throws Exception {

        //BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 

        //String sentence = "This is client 1.";//inFromUser.readLine();        
        //sendData =  sentence.getBytes();
        webcam.setViewSize(WebcamResolution.VGA.getSize());
        WebcamPanel panel = new WebcamPanel(webcam, false);
        WebcamPanel.Painter painter = panel.getPainter();
        panel.setPainter(painter);
        panel.start();
        stream = getStream();

        //while (stream != null) {

        Thread thread = new Thread(new Runnable() {
            public void run() {
                while (true) {
                    DatagramSocket clientSocket;
                    try {
                        clientSocket = new DatagramSocket();
                        InetAddress IPAddress;

                        IPAddress = InetAddress.getByName("localhost");

                        byte[] receiveData = new byte[60000];
                        byte[] sendData = new byte[60000];
                        DatagramPacket sendPacket = new DatagramPacket(stream, stream.length, IPAddress, 9876);
                        clientSocket.send(sendPacket);
                        System.out.println("sending");
                    } catch (SocketException ex) {
                        Logger.getLogger(UDPSender.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (UnknownHostException ex) {
                        Logger.getLogger(UDPSender.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(UDPSender.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    try {
                        // clientSocket.close();
                        stream = getStream();
                    } catch (IOException ex) {
                        Logger.getLogger(UDPSender.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    try {
                        Thread.sleep(1000 / 30);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(UDPSender.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        thread.start();


    }

    public static byte[] getStream() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(baos);


        ImageIO.write(webcam.getImage(), "jpg", bos);
        return baos.toByteArray();
    }
}
